<template>
  <div>
      <div class="crumbs">
          <el-breadcrumb separator="/">
              <el-breadcrumb-item>
                  <i class="el-icon-lx-cascades"></i> 家族成员表格
              </el-breadcrumb-item>
          </el-breadcrumb>
      </div>
      <div class="container">
          <el-table
              :data="tableData"
              border
              class="table"
              ref="multipleTable"
              header-cell-class-name="table-header"
              @selection-change="handleSelectionChange"
          >
            <el-table-column prop="Id" label="编号" width="100" align="center"></el-table-column>
              <el-table-column prop="F_name" label="姓名"></el-table-column>
              <el-table-column prop="Birth_data" label="出生日期"></el-table-column>
              <el-table-column prop="Life" label="生平"></el-table-column>
              <el-table-column prop="Glory" label="荣誉"></el-table-column>
              <el-table-column label="操作" width="180" align="center">
                  <template v-slot="scope">
                      <el-button
                          type="text"
                          icon="el-icon-edit"
                          @click="handleEdit(scope.$index, scope.row)"
                      >编辑</el-button>
                    <el-button
                        type="primary"
                        icon="el-icon-delete"
                        class="red"
                        @click="handleDelete(scope.row)"
                    >删除</el-button>
                  </template>



              </el-table-column>
          </el-table>
          <div class="pagination">
              <el-pagination
                  background
                  layout="total, prev, pager, next"
                  :current-page="query.pageIndex"
                  :page-size="query.pageSize"
                  :total="pageTotal"
                  @current-change="handlePageChange"
              ></el-pagination>
          </div>



<!--添加弹出框-->


        <el-button type="text" @click="dialogFormVisible = true;">添加数据</el-button>
        <el-dialog title="添加家族成员" :visible.sync="dialogFormVisible">
          <el-form :model="form" label-width="70px">
            <el-form-item label="成员编号">
              <el-input v-model="partsData.Id"></el-input>
            </el-form-item>
            <el-form-item label="成员姓名">
              <el-input v-model="partsData.F_name"></el-input>
            </el-form-item>
            <el-form-item label="出生日期">
              <el-input v-model="partsData.Birth_data"></el-input>
            </el-form-item>
            <el-form-item label="生平经历">
              <el-input v-model="partsData.Life"></el-input>
            </el-form-item>
            <el-form-item label="荣誉">
              <el-input v-model="partsData.Glory"></el-input>
            </el-form-item>

          </el-form>

          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveInsert">确 定</el-button>
          </div>
        </el-dialog>

      </div>



      <!-- 编辑弹出框 -->
      <el-dialog title="编辑数据" :visible.sync="editVisible" width="30%">
          <el-form ref="form" :model="form" label-width="70px">
              <el-form-item label="成员编号">
                  <el-input v-model="partsData.Id"></el-input>
              </el-form-item>
      <el-form-item label="成员姓名">
                  <el-input v-model="partsData.F_name"></el-input>
              </el-form-item>
      <el-form-item label="出生日期">
                  <el-input v-model="partsData.Birth_data"></el-input>
        </el-form-item>
              <el-form-item label="生平经历">
              <el-input v-model="partsData.Life"></el-input>
              </el-form-item>
              <el-form-item label="荣誉">
              <el-input v-model="partsData.Glory"></el-input>
              </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
              <el-button @click="editVisible = false">取 消</el-button>
              <el-button type="primary" @click="saveEdit">确 定</el-button>
          </span>
      </el-dialog>
  </div>
</template>

<script>
    import { familydelete,fetchgetfamily,familyinsert } from '../../api/index';
    import { fetchupdatefamily } from '../../api/index';
export default {
  name: 'basetable',
  data() {
      return {
          tableData: [],
          tempPartsData:{},
          partsData: {
            Id:'',
            F_name:'',
            Birth_data:'',
            Life:"",
            Glory:"",

          },

          multipleSelection: [],
          delList: [],
          query: {},
          editVisible: false,
        // userForm:{
        //   username:'',
        //   userid:'',
        //   birth:'',
        //   introduction:'',
        //   honor:"",
        // },



          insertVisible:false,
          pageTotal: 0,
          form: {},
          idx: -1,
          idy: -1,
          id: -1,
          dialogFormVisible: false,
      };
  },
  created() {
     this.getData()
  },
  methods: {
      // 获取 easy-mock 的模拟数据
      getData() {
        fetchgetfamily().then(res =>{console.info(res)
          this.tableData=res.data.value
        })
      },
    handleInsert(index,row) {
      this.idx = index;
      this.partsData = row;
      this.editVisible = true
    },


      handleDelete(row) {
        this.$confirm('此操作会永久删除数据，是否继续？','警告',{
          confirmButtonText:'确定',
          cancelButtonText:'取消',
          type :'warning',
          center : true
        }).then(()=>{
          this.tempPartsData = row
          familydelete(this.tempPartsData).then(res=>{console.info(res)
            console.log(this.tempPartsData)
            console.log(res)
            window.location.reload()
          })
          this.$message({
            type:'success',
            message:'删除成功！'

          });
        }).catch(()=>{
          this.$message({
            type:'info',
            message:'已取消删除'
          });
        });

      },
      // 编辑操作
      handleEdit(index, row) {
          this.idx = index;
          this.partsData = row;
          this.editVisible = true
      },
      // 添加操作
    saveInsert(index, row) {
        this.dialogFormVisible = false;
        this.$message.success("添加成功！");
        familyinsert(this.partsData).then(res => {console.info(res)
          console.log(this.partsData)
          console.log(res)})
    },
      // 保存编辑
      saveEdit() {
          this.editVisible = false;
          this.$message.success(`修改第 ${this.idx + 1} 行成功`);
         fetchupdatefamily(this.partsData).then(res =>{console.info(res)
           console.log(this.partsData)
           console.log(res)
        })
      },
      // 分页导航
      handlePageChange(val) {
          this.$set(this.query, 'pageIndex', val);
          this.getData();
      }
  }
};
</script>

<style scoped>
/*.handle-box {*/
/*    margin-bottom: 20px;*/
/*}*/

/*.handle-select {*/
/*    width: 120px;*/
/*}*/

/*.handle-input {*/
/*    width: 300px;*/
/*    display: inline-block;*/
/*}*/
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #FFFAFA;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>